#name : fun binary prob diff/delta consistent
# key : probdiff.delta.consist
# contributor: Shuguang Sun
# --
${1:bdelta_consist} <- function(n = 20,  r = 1, p1 = 0.2, p2 = 0.1, boundary = 0.05) {

  ## p1 > p2, boundary > 0, to get p1 - p2 > boundary
  n1 <- n * r
  n2 <- 0:n
  p2c <- (0:n) / n

  ne <- ceiling(n1 * (p2c + boundary))
  prob = sum(dbinom(x = n2, size = n, prob = p2) *
               pbinom(q = ne - 1, size = n1, prob = p1, lower.tail = FALSE))

  list(n = n, r = r, p1 = p1, p2 = p2, boundary = boundary, prob =prob)
}

$1(n = 20, r = 1, p1 = 0.2, p2 = 0.1, boundary = 0.05)